<?php defined('SYSPATH') or die('No direct script access.');

 class Controller_Site extends Controller_Base {

    //网站首页
	public function action_index()
	{

        $pageSize=Kohana::$config->load('item.site')?Kohana::$config->load('item.site'):50;

		if($this->request-> is_ajax()){

            $page=intval($this->request->post("page"))?intval($this->request->post("page")):1;

        }
        else{

            $page=intval($this->request->query("page"))?intval($this->request->query("page")):1;

        }

        $page=$page>1?$page:1;

        $offset=$pageSize*($page-1);

        $model=new Model_Site();

        $posts = Cache::instance()->get('site_list'.$offset, null);

        if(empty($posts)){

            $posts=$model->getSitePost($offset,$pageSize);

            Cache::instance()->set('site_list'.$offset, $posts, 100);

        }

        if($this->request-> is_ajax()){

            foreach ($posts as $key => $value) {

                $posts[$key]['image']=Lib_Image::getPostImage($value['id'],$value['images'],260);
                
            }

            $result=array();

            $result['list']=$posts;

            exit(json_encode($result));

        }

        $total = Cache::instance()->get('new_list_items', null);

        if(empty($total)){

            $model=new Model_Site();

            $total=$model->getItems();

            Cache::instance()->set('new_list_items', $total, 100);

        }

        $pages=ceil($total/$pageSize);

        $view=View::factory('site/index');

        $view->page=$page;

        $view->url="/site";

        $view->posts=$posts;

        $view->total=$total;

        $view->pages=$pages;
	
		$this->template->title =__('Home');

		$this->template->content =$view;
		
	 }
 } 
